package net.xrkw.server.module.client.service.softcategory;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import net.xrkw.server.module.client.controller.admin.softcategory.vo.*;
import net.xrkw.server.module.client.dal.dataobject.softcategory.SoftCategoryDO;
import net.xrkw.server.framework.common.pojo.PageResult;
import net.xrkw.server.framework.common.pojo.PageParam;
import net.xrkw.server.framework.common.util.object.BeanUtils;

import net.xrkw.server.module.client.dal.mysql.softcategory.SoftCategoryMapper;

import static net.xrkw.server.framework.common.exception.util.ServiceExceptionUtil.exception;
import static net.xrkw.server.framework.common.util.collection.CollectionUtils.convertList;
import static net.xrkw.server.framework.common.util.collection.CollectionUtils.diffList;
import static net.xrkw.server.module.client.enums.ErrorCodeConstants.*;

/**
 * 软件分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SoftCategoryServiceImpl implements SoftCategoryService {

    @Resource
    private SoftCategoryMapper softCategoryMapper;

    @Override
    public Long createSoftCategory(SoftCategorySaveReqVO createReqVO) {
        // 插入
        SoftCategoryDO softCategory = BeanUtils.toBean(createReqVO, SoftCategoryDO.class);
        softCategoryMapper.insert(softCategory);

        // 返回
        return softCategory.getId();
    }

    @Override
    public void updateSoftCategory(SoftCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateSoftCategoryExists(updateReqVO.getId());
        // 更新
        SoftCategoryDO updateObj = BeanUtils.toBean(updateReqVO, SoftCategoryDO.class);
        softCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoftCategory(Long id) {
        // 校验存在
        validateSoftCategoryExists(id);
        // 删除
        softCategoryMapper.deleteById(id);
    }

    @Override
        public void deleteSoftCategoryListByIds(List<Long> ids) {
        // 删除
        softCategoryMapper.deleteByIds(ids);
        }


    private void validateSoftCategoryExists(Long id) {
        if (softCategoryMapper.selectById(id) == null) {
            throw exception(SOFT_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public SoftCategoryDO getSoftCategory(Long id) {
        return softCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<SoftCategoryDO> getSoftCategoryPage(SoftCategoryPageReqVO pageReqVO) {
        return softCategoryMapper.selectPage(pageReqVO);
    }

}